#include "tcpclient.h"

TCPClient::TCPClient()
{
    m_socket = new QTcpSocket();//新建当前客户端套接字
    connect(m_socket, &QTcpSocket::readyRead, this, &TCPClient::receiveName);
}

TCPClient::~TCPClient()
{
    delete m_socket;
}

void TCPClient::connectToServer(QString ip, quint16 port)
{
    m_socket->connectToHost(ip, port);
}

void TCPClient::disconnectFromHost()
{
    m_socket->disconnectFromHost();
}

void TCPClient::sendMessage(QString message)
{
    m_socket->write(message.toUtf8());
}

void TCPClient::receiveName()
{
    while (m_socket->canReadLine())
    {
        QString _name = m_socket->readLine().trimmed(); // 读取一行并去除首尾空格
        qDebug() << "Received name:" << _name;
        // 可以在这里设置客户端的名字
        emit clientRename(_name);
    }

}


